# AWS SDK for Python
import boto3
from botocore.exceptions import ClientError
# Image processing
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageOps, ImageEnhance
# Data analysis and visualization
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# Standard Python libraries for web requests and file handling
import requests
import zipfile
import os
import re
import io
# Display utilities
from IPython.display import display
import boto3
import json
import boto3
import pandas as pd
from PIL import Image, ImageDraw, ImageFont
from io import BytesIO
from IPython.display import display
# import libraries
import requests
import zipfile
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
# image stuff
# import face_recognition
from PIL import Image, ImageDraw, ImageFont, ImageFilter, ImageOps, ImageEnhance
# from google.colab.patches import cv2_imshow
def detect_faces(photo, bucket, region):
session = boto3.Session()
client = session.client('rekognition', region_name=region)
response = client.detect_faces(Image={'S3Object':{'Bucket':bucket,'Name':photo}},
Attributes=['ALL'])
print('Detected faces for ' + photo)
for faceDetail in response['FaceDetails']:
print(f"The detected face is between {faceDetail['AgeRange']['Low']} and {faceDetail['AgeRange']['High']} years old")
print('Here are the other attributes:')
print(json.dumps(faceDetail, indent=4, sort_keys=True))
# Access predictions for individual face details and print them
print(f"Gender : {faceDetail['Gender']}" )
print(f"Smile : {faceDetail['Smile']}" )
print(f"Eyeglasses: {faceDetail['Eyeglasses']}" )
print(f"Emotions : {faceDetail['Emotions'][0]}" )
return len(response['FaceDetails'])
photo='Eric_Mayo.jpg'
bucket='mayoe23-final-bucket'
region='us-east-1'
face_count=detect_faces(photo, bucket, region)
print("Faces detected: " + str(face_count))
Detected faces for Eric_Mayo.jpg
The detected face is between 26 and 34 years old
Here are the other attributes:
{
"AgeRange": {
"High": 34,
"Low": 26
},
"Beard": {
"Confidence": 99.98954010009766,
"Value": true
},
"BoundingBox": {
"Height": 0.26029014587402344,
"Left": 0.42233142256736755,
"Top": 0.24128423631191254,
"Width": 0.14568157494068146
},
"Confidence": 99.99990844726562,
"Emotions": [
{
"Confidence": 100.0,
"Type": "HAPPY"
},
{
"Confidence": 0.005650520324707031,
"Type": "CALM"
},
{
"Confidence": 0.004684925079345703,
"Type": "CONFUSED"
},
{
"Confidence": 0.0003427267074584961,
"Type": "SURPRISED"
},
{
"Confidence": 0.0,
"Type": "ANGRY"
},
{
"Confidence": 0.0,
"Type": "DISGUSTED"
},
{
"Confidence": 0.0,
"Type": "FEAR"
},
{
"Confidence": 0.0,
"Type": "SAD"
}
],
"EyeDirection": {
"Confidence": 99.99160766601562,
"Pitch": -14.985841751098633,
"Yaw": 2.6591317653656006
},
"Eyeglasses": {
"Confidence": 100.0,
"Value": true
},
"EyesOpen": {
"Confidence": 98.65709686279297,
"Value": true
},
"FaceOccluded": {
"Confidence": 99.9394760131836,
"Value": false
},
"Gender": {
"Confidence": 99.94911193847656,
"Value": "Male"
},
"Landmarks": [
{
"Type": "eyeLeft",
"X": 0.45851635932922363,
"Y": 0.336690217256546
},
{
"Type": "eyeRight",
"X": 0.5254401564598083,
"Y": 0.34097394347190857
},
{
"Type": "mouthLeft",
"X": 0.4598765969276428,
"Y": 0.42811715602874756
},
{
"Type": "mouthRight",
"X": 0.5156224966049194,
"Y": 0.4317719638347626
},
{
"Type": "nose",
"X": 0.48538362979888916,
"Y": 0.37873783707618713
},
{
"Type": "leftEyeBrowLeft",
"X": 0.43599018454551697,
"Y": 0.3166728615760803
},
{
"Type": "leftEyeBrowRight",
"X": 0.47206202149391174,
"Y": 0.3071371912956238
},
{
"Type": "leftEyeBrowUp",
"X": 0.45369961857795715,
"Y": 0.30266472697257996
},
{
"Type": "rightEyeBrowLeft",
"X": 0.5103328227996826,
"Y": 0.30948349833488464
},
{
"Type": "rightEyeBrowRight",
"X": 0.5520758032798767,
"Y": 0.3239831328392029
},
{
"Type": "rightEyeBrowUp",
"X": 0.5307261943817139,
"Y": 0.3074575960636139
},
{
"Type": "leftEyeLeft",
"X": 0.4471665918827057,
"Y": 0.3365420699119568
},
{
"Type": "leftEyeRight",
"X": 0.4716590344905853,
"Y": 0.33833083510398865
},
{
"Type": "leftEyeUp",
"X": 0.4582666754722595,
"Y": 0.3315728008747101
},
{
"Type": "leftEyeDown",
"X": 0.458595871925354,
"Y": 0.34062814712524414
},
{
"Type": "rightEyeLeft",
"X": 0.5120620131492615,
"Y": 0.34089457988739014
},
{
"Type": "rightEyeRight",
"X": 0.5377001166343689,
"Y": 0.3423546254634857
},
{
"Type": "rightEyeUp",
"X": 0.5254073739051819,
"Y": 0.33584538102149963
},
{
"Type": "rightEyeDown",
"X": 0.5247785449028015,
"Y": 0.3448822796344757
},
{
"Type": "noseLeft",
"X": 0.47496873140335083,
"Y": 0.39331209659576416
},
{
"Type": "noseRight",
"X": 0.49959784746170044,
"Y": 0.39492353796958923
},
{
"Type": "mouthUp",
"X": 0.48596471548080444,
"Y": 0.4150463938713074
},
{
"Type": "mouthDown",
"X": 0.4853469431400299,
"Y": 0.443823903799057
},
{
"Type": "leftPupil",
"X": 0.45851635932922363,
"Y": 0.336690217256546
},
{
"Type": "rightPupil",
"X": 0.5254401564598083,
"Y": 0.34097394347190857
},
{
"Type": "upperJawlineLeft",
"X": 0.4253995418548584,
"Y": 0.348438560962677
},
{
"Type": "midJawlineLeft",
"X": 0.43420660495758057,
"Y": 0.44676294922828674
},
{
"Type": "chinBottom",
"X": 0.4852495491504669,
"Y": 0.4951191544532776
},
{
"Type": "midJawlineRight",
"X": 0.5520117878913879,
"Y": 0.4544123113155365
},
{
"Type": "upperJawlineRight",
"X": 0.5709078907966614,
"Y": 0.3575979769229889
}
],
"MouthOpen": {
"Confidence": 99.60477447509766,
"Value": true
},
"Mustache": {
"Confidence": 99.96615600585938,
"Value": true
},
"Pose": {
"Pitch": 10.909923553466797,
"Roll": 1.3273253440856934,
"Yaw": -5.24214506149292
},
"Quality": {
"Brightness": 79.5440444946289,
"Sharpness": 97.45164489746094
},
"Smile": {
"Confidence": 99.39434814453125,
"Value": true
},
"Sunglasses": {
"Confidence": 99.99996948242188,
"Value": false
}
}
Gender : {'Value': 'Male', 'Confidence': 99.94911193847656}
Smile : {'Value': True, 'Confidence': 99.39434814453125}
Eyeglasses: {'Value': True, 'Confidence': 100.0}
Emotions : {'Type': 'HAPPY', 'Confidence': 100.0}
Faces detected: 1
def compare_faces(bucket, sourceFile, targetFile):
client = boto3.client('rekognition')
response = client.compare_faces(SimilarityThreshold=80,
SourceImage={'S3Object': {'Bucket': bucket, 'Name': sourceFile}},
TargetImage={'S3Object': {'Bucket': bucket, 'Name': targetFile}})
for faceMatch in response['FaceMatches']:
position = faceMatch['Face']['BoundingBox']
print(f'face similarity {faceMatch["Similarity"]:.2f}%')
return faceMatch["Similarity"]
# Specify the S3 bucket name
bucket_name = 'mayoe23-final-bucket'
# S3 object keys (file names in the bucket)
source_file = 'Eric_Mayo.jpg'
target_file = 'Eric_Mayo_aged.jpg'
match_similarity = compare_faces(bucket_name, source_file, target_file)
face similarity 93.13%
def display_images_side_by_side(bucket, image_key1, image_key2, similarity):
s3 = boto3.client('s3')
# Get the images from S3
def get_image_from_s3(bucket, key):
response = s3.get_object(Bucket=bucket, Key=key)
image_data = response['Body'].read()
return Image.open(BytesIO(image_data))
image1 = get_image_from_s3(bucket, image_key1)
image2 = get_image_from_s3(bucket, image_key2)
# Rest of your code to process and display images
width1, height1 = image1.size
width2, height2 = image2.size
new_image = Image.new("RGB", (width1 + width2, max(height1, height2)))
new_image.paste(image1, (0, 0))
new_image.paste(image2, (width1, 0))
# Add text
text = f'{similarity:.2f} % MATCH'
font = ImageFont.truetype(r"C:\Users\ericm\OneDrive\Desktop\Data Management\Roboto-Medium.ttf", size=48) # Update path to font
draw = ImageDraw.Draw(new_image)
text_width, text_height = draw.textsize(text, font)
left, top = 900, 100
draw.rectangle(((left, top), (left + text_width, top + text_height)), fill=(255, 255, 255, 128))
draw.text((left, top), text, font=font, fill=(0, 0, 0, 255))
# Display the image
display(new_image)
# Example usage
# Specify the S3 bucket name
bucket_name = 'mayoe23-final-bucket'
# S3 object keys (file names in the bucket)
image_key1 = 'Eric_Mayo.jpg'
image_key2 = 'Eric_Mayo_aged.jpg'
# similarity = 0.8 # Example similarity score
display_images_side_by_side(bucket_name, image_key1, image_key2, match_similarity)
C:\Users\ericm\AppData\Local\Temp\ipykernel_37592\495844869.py:25: DeprecationWarning: textsize is deprecated and will be removed in Pillow 10 (2023-07-01). Use textbbox or textlength instead. text_width, text_height = draw.textsize(text, font)